package com.zxw.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxw.common.entity.article.count.VisitRankBarCountVO;
import com.zxw.common.entity.article.echarts.EchartsVisitRankBarVO;
import com.zxw.common.entity.gateway.LogEntity;
import com.zxw.common.entity.gateway.LogListQuery;
import com.zxw.common.entity.gateway.LogQuery;
import com.zxw.common.entity.gateway.LogVO;
import com.zxw.common.entity.gateway.count.CountLogEchartsVO;
import com.zxw.common.entity.gateway.count.CountLogVO;
import com.zxw.user.mapper.LogMapper;
import com.zxw.user.service.LogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 访问日志记录信息 服务实现类
 * </p>
 *
 * @author zhanxuewei
 * @since 2022-10-10
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, LogEntity> implements LogService {

    @Autowired
    private LogMapper logMapper;

    /**
     * 添加
     *
     * @param logQuery LogQuery
     * @return 是否成功 true|false
     */
    @Override
    public boolean save(LogQuery logQuery) {
        LogEntity logEntity = new LogEntity();
        BeanUtils.copyProperties(logQuery, logEntity);
        logEntity.setId(null);
        logEntity.setStatus("1");
        return super.save(logEntity);
    }

    /**
     * 批量添加
     *
     * @param list List<LogQuery>
     * @return 是否成功 true|false
     */
    @Override
    public boolean saveBatch(List<LogQuery> list) {
        List<LogEntity> collect = list.stream().map(item -> {
            LogEntity logEntity = new LogEntity();
            BeanUtils.copyProperties(item, logEntity);
            logEntity.setId(null);
            logEntity.setStatus("1");
            return logEntity;
        }).collect(Collectors.toList());
        return super.saveBatch(collect);
    }

    /**
     * 根据ID集合批量删除
     *
     * @param ids id集合
     * @return 是否成功 true|false
     */
    @Override
    public boolean removeByIds(List<String> ids) {
        return super.removeByIds(ids);
    }

    /**
     * 根据ID修改
     *
     * @param logQuery LogQuery
     * @return 是否成功 true|false
     */
    @Override
    public boolean updateById(LogQuery logQuery) {
        LogEntity logEntity = new LogEntity();
        BeanUtils.copyProperties(logQuery, logEntity);
        return super.updateById(logEntity);
    }

    /**
     * 根据ID查询数据
     *
     * @param id ID
     * @return LogDO
     */
    @Override
    public LogEntity getById(String id) {
        return super.getById(id);
    }

    /**
     * 条件分页查询
     *
     * @param logListQuery LogListQuery
     * @return IPage<LogDO>
     */
    @Override
    public IPage<LogEntity> list(LogListQuery logListQuery) {
        IPage<LogEntity> pageInfo = new Page<>(logListQuery.getCurrent(), logListQuery.getSize());
        QueryWrapper<LogEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(LogEntity::getStatus, 1)
                .orderByDesc(LogEntity::getCreateTime);
        return super.page(pageInfo, queryWrapper);
    }

    @Override
    public IPage<LogVO> pageList(LogListQuery logListQuery) {
        IPage<LogEntity> pageInfo = new Page<>(logListQuery.getCurrent(), logListQuery.getSize());
        QueryWrapper<LogEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<LogEntity> lambda = queryWrapper.lambda();
        if (StringUtils.isNotBlank(logListQuery.getIp())) {
            lambda.like(LogEntity::getId, logListQuery.getIp());
        }
        if (StringUtils.isNotBlank(logListQuery.getUrl())) {
            lambda.like(LogEntity::getUrl, logListQuery.getUrl());
        }
        lambda.orderByDesc(LogEntity::getCreateTime);
        IPage<LogEntity> page = super.page(pageInfo, queryWrapper);
        return page.convert(logEntity -> {
            LogVO logVO = new LogVO();
            BeanUtils.copyProperties(logEntity, logVO);
            return logVO;
        });
    }

    @Override
    public List<CountLogVO> countLog() {
        return logMapper.countLog();
    }

    @Override
    public CountLogEchartsVO countLogEcharts() {
        CountLogEchartsVO result = new CountLogEchartsVO();

        List<CountLogVO> list = logMapper.countLog();
        if (CollUtil.isNotEmpty(list)) {
            List<String> names = list.stream().map(CountLogVO::getDate).collect(Collectors.toList());
            result.setDates(names);
            List<Long> counts = list.stream().map(CountLogVO::getNum).collect(Collectors.toList());
            result.setNums(counts);
        } else {
            result.setDates(Collections.emptyList());
            result.setNums(Collections.emptyList());
        }
        return result;
    }

    @Override
    public List<VisitRankBarCountVO> visitRankBarCount() {
        return logMapper.visitRankBarCount();
    }

    /**
     * 访问排名前十
     *
     * @return
     */
    @Override
    public EchartsVisitRankBarVO echartsVisitRankBar() {
        EchartsVisitRankBarVO result = new EchartsVisitRankBarVO();
        List<VisitRankBarCountVO> list = visitRankBarCount();
        if (CollUtil.isNotEmpty(list)) {
            List<String> ips = list.stream().map(VisitRankBarCountVO::getIp).collect(Collectors.toList());
            result.setIps(ips);
            List<Long> nums = list.stream().map(VisitRankBarCountVO::getNum).collect(Collectors.toList());
            result.setNums(nums);
        } else {
            result.setIps(Collections.emptyList());
            result.setNums(Collections.emptyList());
        }
        return result;
    }

}
